package com.hr.dept.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.hr.dept.domain.Dept;
import com.hr.dept.domain.DeptUser;
import com.hr.dept.service.Impl.DeptServiceImpl;
import com.hr.dept.service.Impl.DeptUserRelationshipServiceImpl;
import com.hr.user.domain.User;
import com.hr.user.service.Impl.UserServiceImpl;
import com.hr.utils.BaseServlet;
import com.hr.utils.PageBean;

public class DeptUserRelationship extends BaseServlet {
	private DeptServiceImpl deptServiceImpl = new DeptServiceImpl();
	private UserServiceImpl userServiceImpl = new UserServiceImpl();
	private DeptUserRelationshipServiceImpl deptUserRelationshipServiceImpl = new DeptUserRelationshipServiceImpl();
	
	/**
	 * 部门调任前的准备
	 * @param request
	 * @param response
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
	public String deptUserChangePre(HttpServletRequest request, HttpServletResponse response) 
			throws ServletException, IOException {
		return "f:/DeptUserRelationship?method=queryAllpage";
	}
	
	/**
	 * 实现部门的查询分页
	 * @param request
	 * @param response
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
	public String queryAllpage(HttpServletRequest request, HttpServletResponse response) 
			throws ServletException, IOException {
		/**
		 * 1、获取页面传递的PageCurrent
		 * 2、给定PageSize的值
		 * 3、使用PageCurrent和PageSize调用Service方法，得到PageBean，保存request域
		 * 4、转发到/admin/dept_info.jsp
		 */
		int PageCurrent = getPageCurrent(request);//获取当前页
		int PageSize = 5;//获取每页的记录数
		PageBean<Dept> pageBean = deptServiceImpl.queryAll_Page(PageCurrent, PageSize);
		pageBean.setUrl(getUrl(request));
		request.setAttribute("pb", pageBean);
		return "f:/dept/dept_info.jsp";
	}
	
	
	/**
	 * 获取当前的页面值
	 * @param request
	 * @return
	 */
	private int getPageCurrent(HttpServletRequest request){
		String value = request.getParameter("PageCurrent");
		if(value == null || value.trim().isEmpty()){
			return 1;
		} else{
			return Integer.parseInt(value);
		}
	}
	/**
	 * 截取url
	 * @param request
	 * @return
	 */
	public String getUrl(HttpServletRequest request){
		String contextPath = request.getContextPath();//获取项目名
		String servletPath = request.getServletPath();//获取servletPath，即/CustomerServlet
		String queryString = request.getQueryString();//获取问号之后的参数部份
		
		//  判断参数部份中是否包含pc这个参数，如果包含，需要截取下去，不要这一部份。
		if(queryString.contains("&PageCurrent=")) {
			int index = queryString.lastIndexOf("&PageCurrent=");
			queryString = queryString.substring(0, index);
		}
		
		return contextPath + servletPath + "?" + queryString;
	}
	/**
	 * 查看选择的部门下的员工列表
	 * @param request
	 * @param response
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
	public String deptUserShow(HttpServletRequest request, HttpServletResponse response) 
			throws ServletException, IOException {
		String dept_id = request.getParameter("dept_ID");
		Dept dept = deptServiceImpl.findDeptByID(dept_id);
		int PageCurrent = getPageCurrent(request);//获取当前页
		int PageSize = 5;//获取每页的记录数
		PageBean<User> pageBean = deptUserRelationshipServiceImpl.queryAll_Page_Dept_User(PageCurrent,PageSize,dept_id); 
		pageBean.setUrl(getUrl(request));
		request.setAttribute("pb", pageBean);
		request.setAttribute("dept", dept);
		request.setAttribute("msg", dept.getDept_NAME());
		return "f:/dept/dept_user_info.jsp";
	}
	/**
	 * 执行调任前准备
	 * @param request
	 * @param response
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
	public String updateDeptUserPre(HttpServletRequest request, HttpServletResponse response) 
			throws ServletException, IOException {
		String dept_id = request.getParameter("dept_ID");
		String user_id = request.getParameter("user_ID");
		List<Dept> dept_list = deptServiceImpl.queryALL();
		request.setAttribute("dept_list", dept_list);
		User user = userServiceImpl.findByUserID(user_id);
		request.setAttribute("user", user);
		request.setAttribute("dept_id_before", dept_id);
		return "f:/dept/dept_change.jsp";
	}
	
	/**
	 * 实现部门调任
	 * @param request
	 * @param response
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
	public String changeDpetUser(HttpServletRequest request, HttpServletResponse response) 
			throws ServletException, IOException {
		String user_id = request.getParameter("user_id");
		String dept_id_before = request.getParameter("dept_id_before");
		String dept_id_after = request.getParameter("dept_id_after");
		String describle = request.getParameter("describle");
		deptUserRelationshipServiceImpl.changeDeptUser(user_id,dept_id_before,dept_id_after,describle);
		
		request.setAttribute("msg", "员工调任成功,请前往调任部门查看");
		return "f:/DeptUserRelationship?method=deptUserShow&dept_ID="+dept_id_before;
	}
	
	/**
	 * 对于所有的员工的记录进行查询
	 * @param request
	 * @param response
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
	public String queryAll_change_record(HttpServletRequest request, HttpServletResponse response) 
			throws ServletException, IOException {
		HttpSession session = request.getSession();
		User user = (User)session.getAttribute("user");
		if(user.getUser_id().equalsIgnoreCase("dbb80172648a445d82579dbda5b74c92")) {
			int PageCurrent = getPageCurrent(request);//获取当前页
			int PageSize = 8;//获取每页的记录数
			PageBean<DeptUser> pageBean = deptUserRelationshipServiceImpl.queryAll_change_record(PageCurrent, PageSize);
			pageBean.setUrl(getUrl(request));
			request.setAttribute("pb", pageBean);
		} else {
			int PageCurrent = getPageCurrent(request);//获取当前页
			int PageSize = 5;//获取每页的记录数
			PageBean<DeptUser> pageBean = deptUserRelationshipServiceImpl.queryAll_change_record_ByDeptID(PageCurrent, PageSize, user.getDept_id());
			pageBean.setUrl(getUrl(request));
			request.setAttribute("pb", pageBean);
		}
		
		return "f:/dept/dept_change_record_info.jsp";
	}
	
	/**
	 * 个人的调任记录
	 * @param request
	 * @param response
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
	public String queryPerson_change_record(HttpServletRequest request, HttpServletResponse response) 
			throws ServletException, IOException {
		HttpSession session = request.getSession();
		User user = (User)session.getAttribute("user");
		int PageCurrent = getPageCurrent(request);//获取当前页
		int PageSize = 5;//获取每页的记录数
		PageBean<DeptUser> pageBean = deptUserRelationshipServiceImpl.queryAll_change_record_ByUserID(PageCurrent, PageSize, user.getUser_id());
		pageBean.setUrl(getUrl(request));
		request.setAttribute("pb", pageBean);
		return "f:/dept/dept_change_record_info.jsp";
	}
}
